package com.compty.paint.web.front;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.compty.core.framework.context.spring.SpringContextHolder;
import com.compty.core.framework.util.StringUtil;
import com.compty.core.struts2.BaseAction;
import com.compty.paint.domain.Gallery;
import com.compty.paint.domain.Painter;
import com.compty.paint.domain.Painting;
import com.compty.paint.domain.ProvinceInfo;
import com.compty.paint.domain.support.PainterAndWorks;
import com.compty.paint.manager.GalleryManager;
import com.compty.paint.manager.PaintingManager;
import com.compty.paint.manager.ProvinceInfoManager;

/**
 * @Todo:画廊信息Action
 * @CreateTime:2011-8-18 上午09:24:32
 */
public class GalleryAction extends BaseAction<Gallery> {

	private static final long serialVersionUID = -5293543663769352148L;
	
	private GalleryManager galleryManager;
	private ProvinceInfoManager provinceInfoManager;
	
	private Gallery gallery;
	

	/**
	 *@Todo:显示画廊列表(按省份显示)
	 *@CreateTime:2011-8-18 上午09:26:49
	 * @return
	 */
	@SuppressWarnings("unchecked")
	public String list() {
		
		List list = galleryManager.listProvinceId();
		
		Map map = new HashMap();
		for (int i = 0; i < list.size(); i++) {
			Map entryObj = (Map) list.get(i);
			Long pId = (Long) entryObj.get("province_id");
			ProvinceInfo provinceInfo = provinceInfoManager.get(pId);
			if( provinceInfo != null){
				String pName = provinceInfo.getName();
				List galleryList = galleryManager.findGalleryByProvinceId(pId);
				map.put(pName, galleryList);
			}
		}
		
		this.gallery = galleryManager.findNewestGallery();
		
		request.setAttribute("list", map);
		
		return "list";
	}
	
	/**
	 *@Todo:按省份列出画廊
	 *@CreateTime:2011-8-25 下午03:05:05
	 * @return
	 */
	@SuppressWarnings("unchecked")
	public String listByProvince(){
		
		Map map = new HashMap();
		
		String pIdStr = request.getParameter("pIdStr");
	
		if( pIdStr != null && !"".equals(pIdStr)){
			if("all".equals(pIdStr)){
				List list = galleryManager.listProvinceId();
				for (int i = 0; i < list.size(); i++) {
					Map entryObj = (Map) list.get(i);
					Long pId = (Long) entryObj.get("province_id");
					ProvinceInfo provinceInfo = provinceInfoManager.get(pId);
					if( provinceInfo != null){
						String pName = provinceInfo.getName();
						List galleryList = galleryManager.findGalleryByProvinceId(pId);
						map.put(pName, galleryList);
					}
				}
			}
			else{
				if( !StringUtil.isDigit(pIdStr) ){
					return ERROR;
				}
				Long pId = Long.parseLong(pIdStr);
				ProvinceInfo provinceInfo = provinceInfoManager.get(pId);
				if( provinceInfo != null){
					String pName = provinceInfo.getName();
					List galleryList = galleryManager.findGalleryByProvinceId(pId);
					map.put(pName, galleryList);
				}
			}
		}
		
		this.gallery = galleryManager.findNewestGallery();
		
		request.setAttribute("list", map);
		
		return "listByProvince";
	}
	

	/**
	 *@Todo:显示画廊详情及画廊所有作品
	 *@CreateTime:2011-8-18 上午09:58:37
	 * @return
	 */
	@SuppressWarnings("unchecked")
	public String showDetail(){
		
		String idStr = request.getParameter("id");
		Long id = Long.valueOf(idStr);
		
		this.gallery = galleryManager.get(id);
		if( gallery != null){
    		
    		 List<Painter> painters = galleryManager.findPaintersByGalleryId(gallery.getId());
    		 
    		 List returnList = new ArrayList();
    		 if(painters != null){
    			for (Painter painter : painters) {
    				PaintingManager paintingManager = SpringContextHolder.getBean("paintingManager");
    				 List<Painting> paintings = paintingManager.findAvailablePaintingsByPainter(painter.getId());
    				 returnList.add(new PainterAndWorks(painter,paintings));
				}
    		 }
    		 
    		 request.setAttribute("returnList",returnList);
    	}

		return "showDetail";
	}

	public Gallery getGallery() {
		return gallery;
	}

	public void setGallery(Gallery gallery) {
		this.gallery = gallery;
	}

	public void setGalleryManager(GalleryManager galleryManager) {
		this.galleryManager = galleryManager;
	}

	public void setProvinceInfoManager(ProvinceInfoManager provinceInfoManager) {
		this.provinceInfoManager = provinceInfoManager;
	}

}
